package com.xc.hotelmgr.webapi.modules.dt.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.xc.hotelmgr.webapi.common.utils.Constant;
import com.xc.hotelmgr.webapi.modules.dt.entity.HtlDto;
import com.xc.hotelmgr.webapi.modules.op.entity.DtUpHtlEntity;
import com.xc.hotelmgr.webapi.modules.op.service.DtUpHtlService;
import com.xc.hotelmgr.webapi.modules.sys.controller.AbstractController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xc.hotelmgr.webapi.modules.dt.entity.HtlEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.HtlService;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;



/**
 * 分销商可分销的酒店
 *
 * @author kemmy
 * @email kemmy@gmail.com
 * @date 2020-09-27 08:59:22
 */
@EnableAsync
@RestController
@RequestMapping("dt/htl")
public class HtlController extends AbstractController {
    @Autowired
    private HtlService htlService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dt:htl:list")
    public R list(@RequestParam Map<String, Object> params){
        String ownerDeptStr = getUserOwnerDept();
        String[] ownerDept = null;
        String dtId = (String) params.get("dtId");
        // 如果选择了机构，查询指定机构的数据
        if(!StringUtils.equals(dtId,getUserDtId().toString())){
            ownerDept = new String[1];
            ownerDept[0] = dtId;
        }else {
            ownerDept = ownerDeptStr.split("\\,");
        }

        params.put("ownerDept",ownerDept);
        PageUtils page = htlService.sqlQueryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/hotel")
    @RequiresPermissions("dt:htl:list")
    public R listHotel(@RequestParam Map<String, Object> params){
        PageUtils page = htlService.sqlQuryHotelPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dt:htl:info")
    public R info(@PathVariable("id") Integer id){
		HtlEntity htl = htlService.sqlGetById(id);

        return R.ok().put("htl", htl);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dt:htl:save")
    public R save(@RequestBody HtlDto htlDto) {
        R r = htlService.prepareSaveHtl(htlDto);
        if((int)r.get("code")== 0) {
            List<HtlEntity> htlEntities = (List<HtlEntity>) r.get("htls");
            htlService.syncSaveHtl(htlEntities);
        }
        r.remove("htls");
        return r;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dt:htl:update")
    public R update(@RequestBody HtlEntity htl){
		htlService.updateById(htl);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dt:htl:delete")
    public R delete(@RequestBody List<HtlEntity> htlEntities){
        List<Long> delIds = Lists.newArrayList();
        int count =0;
        for(HtlEntity htlEntity : htlEntities){
            count =  htlService.sqlQueryStatus(htlEntity.getDtId(),htlEntity.getHotelCode());
            // 如果已经上架到店铺，则需要先下架
            if (count <=0){
                delIds.add(htlEntity.getId());
            }
        }
		htlService.removeByIds(delIds);
        String msg = "";
        int notDel = htlEntities.size() - delIds.size();
        if(notDel >0){
            msg = "成功删除了" + delIds.size() +"个酒店;"  + notDel + "个酒店已经上架，请先让分销商下架";
        }else{
            msg = "成功删除了"+ htlEntities.size() +"个酒店。";
        }
        return R.ok(msg);
    }

}
